package com.xja.utils;

import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * @author 蔡建国
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class EmailUtil {

    private final JavaMailSender mailSender;

    /**
     * 发送验证码
     *
     * @param receiver   接收验证码的邮箱
     * @param verificode 验证码
     */
    public void sendVerificationCode(String receiver, String verificode) throws Exception {
        log.info("Sending verification code to {}: {}", receiver, verificode);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        helper.setFrom("2732545182@qq.com"); // 发件人
        helper.setTo(receiver); // 收件人
        helper.setSubject("验证码"); // 邮件主题
        helper.setText("<!DOCTYPE html>" +
                "<html lang='zh-CN'>" +
                "<head>" +
                "    <meta charset='UTF-8'>" +
                "    <style>" +
                "        body {" +
                "            font-family: Arial, sans-serif;" +
                "            background-color: #f9f9f9;" +
                "            margin: 0;" +
                "            padding: 0;" +
                "        }" +
                "        .email-container {" +
                "            max-width: 600px;" +
                "            margin: 20px auto;" +
                "            background: #ffffff;" +
                "            border-radius: 8px;" +
                "            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);" +
                "            padding: 20px;" +
                "            text-align: center;" +
                "        }" +
                "        .email-header {" +
                "            font-size: 20px;" +
                "            color: #333333;" +
                "            margin-bottom: 10px;" +
                "        }" +
                "        .email-address {" +
                "            font-size: 18px;" +
                "            color: #007bff;" +
                "            text-decoration: none;" +
                "        }" +
                "        .email-code {" +
                "            font-size: 46px;" +
                "            font-weight: bold;" +
                "            color: #333333;" +
                "            margin: 20px 0;" +
                "        }" +
                "        .email-footer {" +
                "            font-size: 14px;" +
                "            color: #666666;" +
                "        }" +
                "    </style>" +
                "</head>" +
                "<body>" +
                "    <div class='email-container'>" +
                "        <div class='email-header'>" +
                "            您好" +
                "        </div>" +
                "        <div>您的邮箱验证码为：</div>" +
                "        <div class='email-code'>" + verificode + "</div>" +
                "        <div class='email-footer'>5分钟内有效，请勿向他人泄露</div>" +
                "    </div>" +
                "</body>" +
                "</html>", true); // 邮件内容，第二个参数表示是否为 HTML 格式

        mailSender.send(mimeMessage);
    }
}
